package com.Policing.Check;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.Policing.Adapter.Adper;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.Since_case.Since_case;
import com.Policing.bean.Check_adap_bean;
import com.Policing.bean.Check_choose_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2019/5/14.
 */

public class Check_choose extends Activity {
    private ListView listView;
    private String url = "http://"+ Ip_Set.IP+"/api/checkdetailQueryById";
    private Button button1;
    private Handler handler = new Handler();
    private Button button2;
    private Button button3;
    private ImageView fh;
    private String roleid="";
    private List<String> check_id = new ArrayList<>();
    private List<Check_adap_bean> check = new ArrayList<>();
    private List<String> CheckdetailId = new ArrayList<>();
    private String checkId;
    private String id;
    private BaseAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_choose);
        SharedPreferences mm = getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
        id = mm.getString("id","");
        roleid = mm.getString("roleid","");
        inview();
        load();
    }

    private void load() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
                FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
                formBody.add("checkId",checkId)
                        .add("id",id)//传递键值对参数
                        .add("source", Since_case.set().getSource());
                final Request request = new Request.Builder()//创建Request 对象。
                        .url(url)
                        .post(formBody.build())//传递请求体
                        .build();
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //回调方法的使用与get异步请求相同
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Check_choose.this,"请连接网络后重试",Toast.LENGTH_SHORT).show();

                            }
                        });

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String responseData = response.body().string();
                            Log.i("321321",responseData);
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                List<Check_choose_bean> userList = gson.fromJson(responseData, new TypeToken<List<Check_choose_bean>>(){}.getType());
                                for(int i = 0;i < userList.size();i++){
                                    check.add(new Check_adap_bean(false,userList.get(i).getDeptId(),userList.get(i).getType(),userList.get(i).getPlannum(),userList.get(i).getResult()));
                                    check_id.add(userList.get(i).getCheckdetailId());
                                }
                                adapter = new Adper(check,Check_choose.this);
                                listView.setAdapter(adapter);
                            }
                        });

                    }
                });

            }
        }).start();
    }

    private void inview() {
        Intent intent = getIntent();
        checkId = intent.getStringExtra("checkId");
        button3 = (Button) findViewById(R.id.button3);
        fh = (ImageView) findViewById(R.id.fh);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        listView = (ListView) findViewById(R.id.listveiw);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Adper.ViewHolder viewHolder = (Adper.ViewHolder) view.getTag();
                viewHolder.checkBox.toggle();
                check.get(position).setIscheck(viewHolder.checkBox.isChecked());

                if (viewHolder.checkBox.isChecked() == true) {
                    CheckdetailId.add(check_id.get(position));
                } else {
                    CheckdetailId.remove(check_id.get(position));
                }
                // 用TextView显示
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckdetailId.clear();
                for (int i = 0; i < check.size(); i++) {
                    // 改值
                    if (check.get(i).ischeck()) {
                        check.get(i).setIscheck(false);
                    } else {
                        check.get(i).setIscheck(true);
                    }
                    // 刷新
                    adapter.notifyDataSetChanged();
                    // 如果为选中
                    if (check.get(i).ischeck()) {
                        CheckdetailId.add(check_id.get(i));

                    }else CheckdetailId.remove(check_id.get(i));
                }
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int j = 0;
                for (int i = 0; i < check.size(); i++){
                    if (check.get(i).ischeck()) {
                        j++;
                    }
                }
                if (j<check.size()){
                    CheckdetailId.clear();
                    for (int i = 0; i < check.size(); i++) {
                        // 改变boolean
                        check.get(i).setIscheck(true);
                        // 如果为选中
                        if (check.get(i).ischeck()) {
                            CheckdetailId.add(check_id.get(i));
                        }
                    }
                    // 刷新
                    adapter.notifyDataSetChanged();
                    // 显示
                }else {
                    for (int i = 0; i < check.size(); i++) {
                        // 改变boolean
                        check.get(i).setIscheck(false);
                        // 如果为选中
                        if (!check.get(i).ischeck()) {
                            CheckdetailId.remove(check_id.get(i));
                        }
                    }
                    // 刷新
                    adapter.notifyDataSetChanged();
                    // 显示
                }

            }
        });
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (roleid.equals("1")){
                    if (CheckdetailId.size()==1){
                        Intent intent = new Intent(Check_choose.this,Check.class);
                        intent.putExtra("id",checkId);
                        intent.putExtra("checkId",CheckdetailId.toString());
                        startActivity(intent);
                        finish();

                    }else {
                        Toast.makeText(Check_choose.this,"一次只能盘点一个部门",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Intent intent = new Intent(Check_choose.this,Check.class);
                    intent.putExtra("id",checkId);
                    intent.putExtra("checkId",CheckdetailId.toString());
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

}
